package org.nch.videoLauncher.view;

import org.nch.videoLauncher.domain.VideoItem;
import org.nch.videoLauncher.presenter.BrowseVideoPresenter;
import org.nch.videoLauncher.presenter.VideoDetailsPresenter;

import javax.swing.*;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import com.jgoodies.forms.layout.CellConstraints;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 19, 2008
 * Time: 1:59:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class BrowseVideoView implements IContentPanel, IBrowseVideoView {
    private JPanel mainPanel;
    private JTree videoTree;
    private JButton playVideoButton;
    private JPanel itemDetailsPanel;
    private JScrollPane videoTreeScrollPane;
    private BrowseVideoPresenter presenter;
    private VideoDetailsView videoDetailsView;

    private List<VideoItem> videoList;
    private List<VideoItem> movieList;
    private List<VideoItem> tvshowList;
    private List<VideoItem> sportseventList;

    public BrowseVideoView() {

        videoTree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent event) {
                TreePath[] paths = event.getPaths();

                            // Iterate through all affected nodes
                            for (int i=0; i<paths.length; i++) {
                                if (event.isAddedPath(i)) {
                                    if (videoTree.getSelectionPath().getLastPathComponent() instanceof VideoItem){
                                        presenter.showDetailsView((VideoItem)videoTree.getSelectionPath().getLastPathComponent());
                                    }
                                } else {
                                    // This node has been deselected
                                }
                            }
           }
        });
    }

    public void showDetailsView(IContentPanel detailsView) {

        itemDetailsPanel.removeAll();
        CellConstraints cc = new CellConstraints();
        itemDetailsPanel.add(detailsView.getRootComponent(), cc.xy(1,1));
        itemDetailsPanel.updateUI();    // tell (jgoodies) f        
    }

    public JPanel getRootComponent() {
        return mainPanel;
    }

    public void setMovieList(List<VideoItem> videoItems) {
        this.movieList = videoItems;
    }

    public void setSportsEventList(List<VideoItem> videoItems) {
        this.sportseventList = videoItems;
    }

    public void setTvShowList(List<VideoItem> videoItems) {
        this.tvshowList = videoItems;
    }


    public VideoItem getCurrentSelection() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setPresenter(BrowseVideoPresenter presenter) {
        this.presenter = presenter;
    }

    private class videoTreeModel implements TreeModel {

        private DefaultMutableTreeNode moviesParentNode;
        private DefaultMutableTreeNode tvshowsParentNode;
        private DefaultMutableTreeNode sportsEventsParentNode;
        private DefaultMutableTreeNode allVideos;
        private ArrayList<DefaultMutableTreeNode> listParents;

        public videoTreeModel(){

            listParents = new ArrayList<DefaultMutableTreeNode>();
            allVideos = new DefaultMutableTreeNode("All Videos");
            moviesParentNode = new DefaultMutableTreeNode("Movies (" + movieList.size() + ")");
            listParents.add(moviesParentNode);
            tvshowsParentNode = new DefaultMutableTreeNode("TV Shows (" + tvshowList.size() + ")");
            listParents.add(tvshowsParentNode);
            sportsEventsParentNode = new DefaultMutableTreeNode("Sports Events (" + sportseventList.size() + ")");
            listParents.add(sportsEventsParentNode);
        }

        public Object getRoot() {
            return allVideos;
        }

        public Object getChild(Object o, int i) {

            if (o == allVideos){
                return listParents.get(i);
            }

            if (o == moviesParentNode){
                return movieList.get(i);
            }

            if (o == tvshowsParentNode){
                return tvshowList.get(i);
            }

            if (o == sportsEventsParentNode){
                return sportseventList.get(i);
            }

            return null;
        }

        public int getChildCount(Object o) {

            if (o == allVideos){
                return listParents.size();
            }

            if (o == moviesParentNode){
                return movieList.size();
            }

            if (o == tvshowsParentNode){
                return tvshowList.size();
            }

            if (o == sportsEventsParentNode){
                return sportseventList.size();
            }

            return 0;
        }

        public boolean isLeaf(Object o) {

            if (o instanceof VideoItem)
                return true;
            else
                return false;

        }

        public void valueForPathChanged(TreePath treePath, Object o) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public int getIndexOfChild(Object o, Object o1) {

            if (o == allVideos){
                return listParents.indexOf(o1);
            }

            if (o == moviesParentNode){
                return movieList.indexOf(o1);
            }

            if (o == tvshowsParentNode){
                return tvshowList.indexOf(o1);
            }

            if (o == sportsEventsParentNode){
                return sportseventList.indexOf(o1);
            }

            return 0;

        }

        public void addTreeModelListener(TreeModelListener treeModelListener) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void removeTreeModelListener(TreeModelListener treeModelListener) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }

    public void showVideoListData() {
        videoTree.setModel(new videoTreeModel());
    }
}
